﻿using AutoMapper;
using Produce.Application.DTO.Input;
using Produce.Domain.Entities;
using Produce.Infrastructure.Repositories;
using Produce.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Produce.Application.Services
{
    public class ProduceStateService : IProduceStateService
    {
        private readonly IMapper _mapper;
        private readonly IProduceModelRepository<ProductionState> _StateReq;

        public ProduceStateService(IProduceModelRepository<ProductionState> StateReq, IMapper mapper)
        {
            _mapper = mapper;
            _StateReq = StateReq;
        }

        /// <summary>
        /// 删除产品锁定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> DelState(long id)
        {
            var model = _StateReq.GetValue().Where(x => x.Id == id).First();

            if (model == null)
            {
                return new ApiResult<int>
                {
                    Success = false,
                    Message = "产品锁定不存在",
                    Data = 0
                };
            }

            model.IsDeleted = true;

            _StateReq.Update(model);

            return new ApiResult<int>
            {
                Success = true,
                Message = "删除产品锁定成功",
                Data = 1
            };
        }

        /// <summary>
        /// 产品锁定的列表  （State是false）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<ProductionState> GetProduceStateFalse(GetProduceStateInput input)
        {
            try
            {

                var query = _StateReq.GetValue().Where(x => x.ProductionType == input.ProductionType &&
                                      x.ProductionCode == input.ProductionCode &&
                                      x.IsDeleted == false &&
                                      x.ProduceState == false);

                if (!string.IsNullOrEmpty(input.Process))
                {
                    query = query.Where(x => x.Process == input.Process);
                }

                if (input.ProductionDateTime != null)
                {
                    query = query.Where(x => x.ProductionDateTime == input.ProductionDateTime);
                }

                return new ApiResult<ProductionState>
                {
                    Data = query.First(),
                    Success = true,
                    Message = "查询成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<ProductionState>
                {
                    Data = null,
                    Success = false,
                    Message = $"查询失败：{ex.Message}"
                };
            }
        }
        /// <summary>
        /// 产品解锁的列表  （State是true）
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<List<ProductionState>> GetProduceStateTrue(PageInput input)
        {
            var query = _StateReq.GetValue().Where(x => x.IsDeleted == false && x.ProduceState == true);


            //倒叙显示
            var list = query.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();

            return new ApiResult<List<ProductionState>>
            {
                Data = list,
                Success = true,
                Message = "查询成功"
            };

        }
        /// <summary>
        /// 解锁产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateFalseState(long id)
        {
            var model = _StateReq.GetValue().Where(x => x.Id == id).First();

            if (model == null)
            {
                return new ApiResult<int>
                {
                    Success = false,
                    Message = "解锁产品不存在",
                    Data = 0
                };
            }

            model.ProduceState = false;

            _StateReq.Update(model);

            return new ApiResult<int>
            {
                Success = true,
                Message = "解锁产品成功",
                Data = 1
            };
        }
        /// <summary>
        /// 锁定产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateTrueState(long id,string desc)
        {
            var model = _StateReq.GetValue().Where(x => x.Id == id).First();

            if (model == null)
            {
                return new ApiResult<int>
                {
                    Success = false,
                    Message = "锁定产品不存在",
                    Data = 0
                };
            }

            model.ProduceState = true;
            model.ProductionDesc = desc;

            _StateReq.Update(model);

            return new ApiResult<int>
            {
                Success = true,
                Message = "锁定产品成功",
                Data = 1
            };
        }
    }
}
